﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class SymptomAdd : System.Web.UI.Page
    {
        private string URL_BACK = "Symptom.aspx";
        private SymptomBLL symptomBll = SymptomBLL.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void typeListBound(object sender, EventArgs e)
        {
            try
            {
                //绑定类型列表值
                DropDownList list = (DropDownList)(((DetailsView)sender).FindControl("typeList"));
                list.DataSource = symptomBll.GetSymptomTypeList(0); //获得一级分类
                list.DataTextField = "Name";
                list.DataValueField = "Id";
                list.DataBind();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void InsertButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                SymptomInfo info = validateForm(this.symptomInfo);

                if (info != null)
                {
                    //准备数据,调用SqlServerDAL执行增加
                    if (symptomBll.AddSymptom(info))  //成功
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        //数据有效性检查
        private SymptomInfo validateForm(DetailsView formViewCtl)
        {
            SymptomInfo info = new SymptomInfo();

            info.Name = ((TextBox)formViewCtl.FindControl("name")).Text;
            info.Intro = ((TextBox)formViewCtl.FindControl("introduction")).Text;
            info.MildDescription = ((TextBox)formViewCtl.FindControl("mildDescription")).Text;
            info.ModerateDescription = ((TextBox)formViewCtl.FindControl("moderateDescription")).Text;
            info.SevereDescription = ((TextBox)formViewCtl.FindControl("severeDescription")).Text;
            info.Type = Convert.ToInt32(((DropDownList)formViewCtl.FindControl("typeList")).SelectedValue);
            info.PinYin = Chinese.GetPingYin(info.Name);
            info.Desp = ((TextBox)formViewCtl.FindControl("description")).Text;
            info.UpdateTime = DateTime.Now;

            return info;
        }
    }
}
